﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;

namespace team1337blarg
{
    public class NicksGame : GameScreen
    {
        ContentManager content;
        //GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D background;
        Texture2D stickguy1;
        Texture2D bullet1;
        StickSprite stickGuy = new StickSprite();

        int bulletX;
        int bulletY = 40;

        public NicksGame()
        {
            stickGuy.startingPositionX = 0;
            stickGuy.startingPositionY = 0;
            stickGuy.SpriteWidth = 60;
            stickGuy.SpriteHeight = 80;
            stickGuy.SpriteStartX = 380;
            stickGuy.SpriteStartY = 270;

           //graphics = new GraphicsDeviceManager(this);
        }
        /*
        protected override void Initialize()
        {
            base.Initialize();
        }
        */
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");
            
            // Create a new SpriteBatch, which can be used to draw textures.
            //graphics.PreferredBackBufferWidth = 800;
            //graphics.PreferredBackBufferHeight = 600;
            //graphics.ApplyChanges();
            spriteBatch = ScreenManager.SpriteBatch;
            background = content.Load<Texture2D>(@"Nick/Textures/texture1");
            stickguy1 = content.Load<Texture2D>(@"Nick/Textures/stickguy1");
            bullet1 = content.Load<Texture2D>(@"Nick/Textures/bullet1");

            Thread.Sleep(1000);
            ScreenManager.Game.ResetElapsedTime();
        }

        public override void UnloadContent()
        {
            content.Unload();
        }


        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, false);

            if (stickGuy.spriteDirection == true)
                bulletX++;
            else
                bulletX--;

            //if (Keyboard.GetState().IsKeyDown(Keys.Escape))
               // this.Exit();
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
                stickGuy.SpriteStartY -= 10;
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
                stickGuy.SpriteStartY += 10;
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                stickGuy.SpriteStartX -= 10;
                stickGuy.spriteDirection = false;
                bulletX = -3;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                stickGuy.SpriteStartX += 10;
                stickGuy.spriteDirection = true;
                bulletX = 57;
            }

            if (stickGuy.spriteDirection == true)
                stickGuy.spriteSizeIncX = 0;

            if (stickGuy.spriteDirection == false)
                stickGuy.spriteSizeIncX = 60;

            if ((Keyboard.GetState().IsKeyDown(Keys.X)))
                stickGuy.gunShot = true;
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.CornflowerBlue, 0, 0);

            spriteBatch.Begin();

            spriteBatch.Draw(
            background,
            ScreenManager.GraphicsDevice.Viewport.Bounds,
            Color.White);

            spriteBatch.Draw(
            stickguy1,
            new Rectangle(stickGuy.SpriteStartX, stickGuy.SpriteStartY, 60, 80),
            new Rectangle(stickGuy.spriteSizeIncX, 0, 60, 80),
            Color.White);

            if (stickGuy.gunShot == true)
            {
                spriteBatch.Draw(
                bullet1,
                new Rectangle(stickGuy.SpriteStartX + bulletX, stickGuy.SpriteStartY + 40, 7, 3),
                new Rectangle(0, 0, 7, 3),
                Color.White);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
